#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import socket


def sockget(s, length):
    """Read socket till length reached or connection closed"""

    dat = b""
    left = length
    while left:
        try:
            tmp = s.recv(left)
        except socket.error as err:
            if err.errno == 11:
                continue
            raise
        except:
            raise

        if not tmp:
            return None
        left -= len(tmp)
        dat += tmp
    return dat.decode("utf-8")
